package com.xqt.service.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.xqt.base.dao.BaseDao;
import com.xqt.base.entity.BaseEntity;
import com.xqt.base.service.BaseService;
import com.xqt.config.XqtTransactionalControl;
import com.xqt.constant.Globals;
import com.xqt.dao.sys.SysRoleDao;
import com.xqt.entity.sys.SysRole;
import com.xqt.entity.sys.SysRoleMember;
import com.xqt.entity.sys.SysRoleMenu;
import com.xqt.inters.service.sys.ISysRoleService;
import com.xqt.util.StringUtil;

/**
 * Created by Administrator on 2017/3/21.
 */
public class SysRoleService extends BaseService<SysRole> implements ISysRoleService {

    // 角色操作Dao
    @Autowired
    private SysRoleDao sysRoleDao;

    @Override
    public void add(SysRole t) throws Exception {
        preAdd(t);
        sysRoleDao.add(t);
    }

    public void update(SysRole t) throws Exception {
        preUpdate(t);
        sysRoleDao.updateBySelective(t);
    }

    @Override
    @XqtTransactionalControl
    public void delete(Object... ids) {
        if (ids == null || ids.length < 1) {
            return;
        }
        for (Object id : ids) {
            // 删除角色人员分配记录
            sysRoleDao.deleteRoleMemberByRoleId(id);

            // 删除角色菜单分配记录
            sysRoleDao.deleteRoleMenuByRoleId(id);

            // 删除角色
            sysRoleDao.delete(id);
        }
    }

    @Override
    public void updateBySelective(SysRole sysRole) throws Exception {
        preUpdate(sysRole);
        sysRoleDao.updateBySelective(sysRole);
    }

    @Override
    public int queryByCount(BaseEntity page) {
        return sysRoleDao.queryByCount(page);
    }

    @Override
    public List<SysRole> queryByList(BaseEntity page) {
        return sysRoleDao.queryByList(page);
    }

    @Override
    public SysRole queryByCode(Object code) {
        return sysRoleDao.queryByCode(code);
    }

    @Override
    public int checkRoleExist(SysRole role) throws Exception {
        return sysRoleDao.checkRoleExist(role);
    }

    @Override
    public void allotSave(SysRole role, String checkedMenu) throws Exception {
        // 将原有的分配记录删除
        sysRoleDao.deleteRoleMenuByRoleId(role.getId());

        if (!StringUtil.isEmpty(checkedMenu)) {

            // 将菜单分解
            String[] menuList = checkedMenu.split(Globals.COMMA);

            // 循环插入新的记录
            SysRoleMenu item = null;
            for (String menuId : menuList) {
                item = new SysRoleMenu();
                item.setMenuId(menuId);
                item.setRoleId(role.getId());
                preInsert(item);
                sysRoleDao.addRoleMenu(item);
            }
        }
    }

    @Override
    public void allotMemberSave(SysRole role, String checkedMember) throws Exception {
        // 将原有的分配记录删除
        sysRoleDao.deleteRoleMemberByRoleId(role.getId());

        if (!StringUtil.isEmpty(checkedMember)) {

            // 将菜单分解
            String[] memberList = checkedMember.split(Globals.COMMA);

            // 循环插入新的记录
            SysRoleMember item = null;
            for (String memberId : memberList) {
                item = new SysRoleMember();
                item.setMemberId(memberId);
                item.setRoleId(role.getId());
                preInsert(item);
                sysRoleDao.addRoleMember(item);
            }
        }
    }

    @Override
    public List<SysRoleMember> memberRoleList(SysRoleMember sysRoleMember) {
        return sysRoleDao.memberRoleList(sysRoleMember);
    }

	@Override
	public BaseDao<SysRole> getDao() {
		return sysRoleDao;
	}
}
